package net.oschina.aibinxiao.scheduler;

import net.oschina.aibinxiao.job.HelloJob2;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

public class HelloScheduler2 {
    public static void main(String[] args) throws SchedulerException {
        // 创建一个JobDetail实例，将该实例与HelloJob Class绑定  传入自定义参数
        JobDetail jobDetail = JobBuilder.newJob(HelloJob2.class)
                .withIdentity("myjob2")
                .usingJobData("jobMessage", "hello myjob2")
                .usingJobData("mathJobValue", 3.14F)
                .build();

        // 创建一个Trigger实例，定义该job立即执行，并且每隔两秒执行一次，直到永远
        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("mytrigger2", "group2")
                .usingJobData("triggerMessage", "hello mytrigger2")
                .usingJobData("doubleTriggerValue", 2.1D)
                .startNow()
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                                .withIntervalInSeconds(2)
                                .repeatForever())
                .build();

        // 创建Scheduler实例
        SchedulerFactory sfact = new StdSchedulerFactory();
        Scheduler scheduler = sfact.getScheduler();
        scheduler.start();

        // 打印当前时间，格式为 yyyy-MM-dd HH:mm:ss
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(date);
        System.out.println("当前时间是：" + dateStr);

        scheduler.scheduleJob(jobDetail, trigger);
    }
}
